import moment from 'moment'
import PubSub from 'pubsub-js'
import { requestData } from "../../config/requestData"
import { songUrl, urlUrl } from '../../config/api'
let app = getApp()
// pages/songDetail/songDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        songList: [], //歌曲
        startTime: '00:00',
        totalTime: '00:00',
        isPlay: false, //是否播放
        progressWidth: 0, //进度条颜色
        musicUrl: '', //音乐url链接
        musicId: '', //音乐id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadingSongData(options.id)
        this.loadingMusicMethod(options.id)
    },
    handleSongDetail(e) {
        const { type } = e.currentTarget.dataset
        this.musicInstace.stop()
        PubSub.subscribe('musicId', (msg, musicId) => {
            this.loadingSongData(musicId)
            this.musicControl(true, musicId)
            PubSub.unsubscribe('musicId')
        })
        PubSub.publish('changeTypeData', type)
    },
    handlePlay () {
        let isPlay = !this.data.isPlay
        // this.setData({
        //     isPlay
        // })
        let { musicId, musicUrl } = this.data
        this.musicControl(isPlay, musicId, musicUrl)
    },
    async musicControl (isPlay, musicId, musicUrl) {
        const options = {
            url: urlUrl,
            data: {
                id: musicId
            }
        }
        if (isPlay){
            if(!musicUrl){
                const res = await requestData(options)
                const { code, data } = res
                if (code === 200) {
                    let musicUrl = data[0].url
                    this.setData({
                        musicUrl
                    })
                }
                this.musicInstace.title = this.data.songList.name
                this.musicInstace.src = this.data.musicUrl
            }else {
                this.musicInstace.pause()
            }
        }
    },
    loadingMusicMethod (id) {
        if(app.globalData.isMusicPlay && app.globalData.musicId === id) {
            this.setData({
                isPlay: true
            })
        }
        this.musicInstace = wx.getBackgroundAudioManager()
        this.musicInstace.onPlay(() => {
            this.changeStatus(true)
            app.globalData.musicId = id
        })
        this.musicInstace.onPause(() => {
            this.changeStatus(false)
        })
        this.musicInstace.onStop(() => {
            this.changeStatus(false)
        })
        this.musicInstace.onEnded(() => {
            PubSub.publish('changeTypeData', 'next') //歌曲自动结束，自动下一首
            this.setData({
                progressWidth: 0,
                startTime: '00:00'
            })
        })
        this.musicInstace.onTimeUpdate(() => {
            let startTime = moment(this.musicInstace.currentTime * 1000).format('mm:ss')
            let totalTime = this.musicInstace.currentTime / this.musicInstace.duration * 400
            this.setState({
                startTime,
                totalTime
            })
        })
    },
    changeStatus (isPlay) {
        this.setData({
            isPlay
        })
        app.globalData.isMusicPlay = isPlay
    },
    async loadingSongData(id) {
        const options = {
            url: songUrl,
            data: {
                ids: id
            }
        }
        const res = await requestData(options)
        const { code, songs } = res
        if(code === 200) {
            wx.setNavigationBarTitle({
              title: songs[0].al.name,
            })
            let totalTime = moment(songs[0].dt).format('mm:ss')
            this.setData({
                songList: songs,
                totalTime
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})